from PIL import Image, ImageFilter, ImageDraw, ImageFont
import random

# 打开一个png图像文件，注意是当前路径:
img = Image.open('index.png')
# 获得图像尺寸:
w, h = img.size
print('原图尺寸 宽：%s 高：%s' % (w, h))
# 缩放到50%:
img.thumbnail((w//2, h//2))
print('缩放后的尺寸: 宽：%s 高：%s' % (w//2, h//2))
# 另存为新图片
img.save('3333.png')

# 打开一个图片
img2 = Image.open('index.png')
# 应用模糊滤镜:
img3 = img2.filter(ImageFilter.BLUR)
# 保存模糊图片
img3.save('1234.png')

# 随机字母
def ran_char():
    return chr(random.randint(65, 90))

# 随机颜色1
def ran_color1():
    return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))

# 随机颜色2
def ran_color2():
    return (random.randint(32, 127), random.randint(64, 255), random.randint(64, 255))

width = 60*4
height = 60
# 创建一张图片
image = Image.new('RGB', (width, height), (255, 255, 255))
# 创建Font对象:
font = ImageFont.truetype('Arial.ttf', 36)
# 创建Draw对象:
draw = ImageDraw.Draw(image)
# 填充每个像素:
for x in range(width):
    for y in range(height):
        draw.point((x, y), fill=ran_color1())
# 输出文字:
for t in range(4):
    draw.text((60*t + 10, 10), ran_char(), font=font, fill=ran_color2())
# 模糊效果
# image = image.filter(ImageFilter.BLUR)
image.save('demo.jpg', 'jpeg')


